
{{template "base.html" .}}

{{define "content"}}

<!--操作按钮模板-->
<input id="Id" value="{{.Id}}" style="display: none">
<input id="Class" value="{{.Class}}" style="display: none">
<input id="Major" value="{{.Major}}" style="display: none">
<input id="Sex" value="{{.Sex}}" style="display: none">
<form class="layui-form" action="" id="Sfrom" style="padding: 10% 20%">
    <div class="layui-form-item">
        <label class="layui-form-label">学号</label>
        <div class="layui-input-block">
            <input type="text" name="Number" required  lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input" value="{{.Number}}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input type="text" name="Name" required  lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input" value="{{.Name}}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
            <input type="text" name="Password" required  lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input" value="{{.Password}}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">年龄</label>
        <div class="layui-input-block">
            <input type="text" name="Age" required  lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input" value="{{.Age}}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">专业</label>
        <div class="layui-input-block">
            <select id="Major_id" lay-verify="required" lay-filter="Major_id" name="Major_id">
                <option value="">请选择</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">班级</label>
        <div class="layui-input-block">
            <select id="Class_id" name="Class_id" lay-verify="required" lay-filter="Class_id">
                <option value="">请选择</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">单选框</label>
        <div class="layui-input-block">
            <input type="radio" name="Sex" value="男" title="男" id="sexRadio1">
            <input type="radio" name="Sex" value="女" title="女" id="sexRadio2">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="articlesub">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script>
    //专业下拉框绑定 fun.js
    QueryMajorId();
    //根据专业的班级下拉框绑定
    var form = layui.form;
    $.ajax({
        url:'/teacher/stu_class',
        method:'get',
        dataType:'JSON',
        success:function(res){
            $("#Class_id option").remove();                                                         //删除之前绑定的选项
            $.each(res.data, function (i, obj) {
                var bing = "";
                if (obj.Id==$("#Class").val()){bing = "selected"}else {bing=""}                     //设置绑定值
                if (obj.MajorId==$("#Major").val()){
                        str = '<option '+bing+' value="' + obj.Id + '">' + obj.Name + '</option>';
                        $(str).appendTo("#Class_id");//绑定
                }
            });//遍历循环遍历
            form.render("select");//注意：最后必须重新渲染下拉框，否则没有任何效果。
        },
    });
    //性别绑定
    if($("#Sex").val() == "男"){
        $("#sexRadio1").prop("checked",true);
        $("#sexRadio2").prop("checked",false);
    }else {
        $("#sexRadio1").prop("checked",false);
        $("#sexRadio2").prop("checked",true);
    }

    //表单提交
    layui.use('form', function(){
        var form = layui.form;
        //动态更新班级选项
        form.on('select(Major_id)', function(data){
            QueryClassId(data.value);
        })
        form.on('submit(articlesub)', function(data){
            $.ajax({
                url:'/teacher/stu/'+$("#Id").val(),
                method:'put',
                data:data.field,
                dataType:'JSON',
                success:function(res){
                    if(res.code=='0'){
                        layer.msg(res.msg,{icon: 1, time: 1000},function(){
                             window.location.replace("/teacher");
                        }); //提交成功后刷新页面
                    }
                    else
                        layer.msg(res.msg,{icon: 1, time: 1000},function(){}); //提交成功后刷新页面
                },
            });
            return false;
        });
    });


</script>
{{end}}
